class Solution {
    public static void main(String[] args) {
        int[] arr = {8828,9581,49,9818,9974,9869,9991,10000,10000,10000,9999,9993,9904,8819,1231,6309};
        System.out.println(minOperations(arr, 134365));
    }
    public static int minOperations(int[] nums, int x) {
        int tmp = 0 ;
        for(int i =0 ; i < nums.length ; i ++){
            tmp += nums[i];
        }
        tmp -= x ;     // sum-x , 中间那段的长度
        if(tmp < 0) return -1 ;
        int sum = 0 ;
        int ret = -1 ;    //中间那段长度 ,要最长的
        for(int i = 0 , j =0  ; j < nums.length ; j ++){
             //进窗口
             sum += nums[j];
            //出窗口
            while(i < nums.length && sum > tmp){
               sum -= nums[i++];
            }
            if(sum == tmp){
                ret = Math.max(ret , j-i+1);
            }
        }
        if(ret == -1){
            return -1 ;
        }
        return nums.length - ret ;
    }
}